package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.dao.ProductTypeMapper;
import com.webfinal.backsystem.entity.ProductType;
import com.webfinal.backsystem.request.prosucttype.AddProTypeRequest;
import com.webfinal.backsystem.request.prosucttype.SearchByNameRequest;
import com.webfinal.backsystem.request.prosucttype.UpdateProTypeRequest;
import com.webfinal.backsystem.response.producttype.AddProTypeResponse;
import com.webfinal.backsystem.response.producttype.SearchByNameResponse;
import com.webfinal.backsystem.response.producttype.UpdateProTypeResponse;
import com.webfinal.backsystem.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public SearchByNameResponse searchProductTypeByName(SearchByNameRequest searchByNameRequest) {
        SearchByNameResponse response = new SearchByNameResponse();
        ProductType productType = productTypeMapper.selectByName(searchByNameRequest.getTypeName());
        if(productType==null){
            response.setResultCode("1");
            response.setResultMsg("类型不存在");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setProductType(productType);
        }
        return response;
    }

    @Override
    public AddProTypeResponse addProductType(AddProTypeRequest addProTypeRequest) {
        AddProTypeResponse addProTypeResponse = new AddProTypeResponse();
       try {
           ProductType productType = new ProductType();
           productType.setTypeId(0);
           productType.setTypeName(addProTypeRequest.getTypeName());
           productType.setTypeCode(addProTypeRequest.getTypeCode());
           productType.setTypeOn( addProTypeRequest.getTypeOn());
           if(productTypeMapper.insert(productType)==1){
               addProTypeResponse.setResultCode("0");
               addProTypeResponse.setResultMsg("success");
           }else {
               addProTypeResponse.setResultCode("1");
               addProTypeResponse.setResultMsg("error");
           }
       }catch (Exception e){
           e.printStackTrace();
       }

        return addProTypeResponse;
    }

    @Override
    public UpdateProTypeResponse updateProductType(UpdateProTypeRequest updateProTypeRequest) {
        UpdateProTypeResponse response = new UpdateProTypeResponse();
        try {
            ProductType productType = productTypeMapper.selectByName(updateProTypeRequest.getTypeName());
            if(productType==null){
                response.setResultCode("1");
                response.setResultMsg("error");
            }else {
                productType.setTypeOn(updateProTypeRequest.getTypeOn());
                if(productTypeMapper.updateByPrimaryKey(productType)==1){
                    response.setResultCode("0");
                    response.setResultMsg("success");
                }else {
                    response.setResultCode("1");
                    response.setResultMsg("error");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return response;
    }
}
